const {getQR,getResult} = require("../modules/bilibili/login/QRlogin")
const rank = require("../modules/kugou/playlist/rank")
const recGedan = require("../modules/kuwo/recGedan")
var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
router.get('/login/QR', function(req, res, next) {
  getQR().then((r) => {
    res.send(r.data)
  })
});
router.post('/login/QRlogin', function(req, res, next) {
  function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }

  async function login() {
    var flag = true;
    do {
      await getResult(req.query).then((r) => {
        if (r.data.data.url !== undefined) {
          res.send(r.data)
          flag = false;
        } else if (r.data.data === -2 || r.data.data === '-2' || r.data.data === -1 || r.data.data === '-1') {
          res.send(r.data)
          console.log("###：尝试登录超时/参数错误")
          flag = false;
        } else {
          console.log("###：尝试登录ing")
        }
      })
      await sleep(3000);
    } while (flag)
  }

  if (req.query.oauthKey === undefined) {
    res.send({
      data: -2
    })
  } else{
    login().then(() => {
      console.log('登陆完毕')
    })
  }
});



module.exports = router;
